using Microsoft.AspNetCore.Mvc;
using MVC高级教程.Models;
using MVC高级教程.Service;
using System.Diagnostics;

namespace MVC高级教程.Controllers
{
    public class HomeController : Controller
    {


        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }


        /**
                Action控制器动作
                    控制器动作(具体的Action)返回的结果，叫做控制器动作结果，动作结果是控制器返回给浏览器请求的内容。
                    以下是常见的动作结果类型
                    ①、ViewResult 返回视图
                    ②、RedirectToActionResult 重定向到某个Action
                    ③、JsonRersult  Json结果
                    ④、ContentResult 文本结果
                    ⑤、RedirectResult  重定向到新的Url
                    ⑥、RedirectToRouteResult 重定向到新的控制器动作(Action)
                    ⑦、FileResult  返回一个文件 
         * */

        /// <summary>
        /// 创建视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 创建视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        /// <summary>
        /// 演示如何从上下文中拿到IServiceProvider接口对象
        /// </summary>
        /// <returns></returns>
        public IActionResult Index7()
        {
            //此方式可拿到服务
            Request.HttpContext.RequestServices.GetService<IFoodService>();
            return View();
        }
    }
}
